import { OnInit, Component, Input, Output, EventEmitter } from '@angular/core';
import { CourseDTO } from 'src/app/project/model/icm/icm-base/course/course.dto';
import { CourseTypeDTO } from 'src/app/project/model/icm/icm-base/course-type/course-type.dto';
import { CourseTypeService } from 'src/app/project/service/icm/icm-base/course-type/course-type.service';

@Component({
  selector: 'app-course-type-select',
  templateUrl: './course-type-select.component.html',
  styles: [],
})
export class CourseTypeSelectComponent implements OnInit {
  constructor(private courseTypeService: CourseTypeService) {}

  @Input()
  courseTypeId: string;
  @Output()
  courseTypeIdChange = new EventEmitter<string>();

  /**
   * 课程类型
   */
  @Input()
  courseType: CourseTypeDTO;

  /**
   * 是否预览
   */
  @Input()
  isPreView: boolean;

  /**
   * 课程类型下拉
   */
  courseTypeList: Array<CourseTypeDTO> = [];

  /**
   * 课程类型下拉加载
   */
  isLoading = false;

  /**
   * 获取课程类型下拉
   */
  getCourseTypeDropDownList() {
    this.courseTypeService.findAllBySort('showOrder', 'asc').subscribe(data => {
      this.isLoading = false;
      this.courseTypeList.forEach(courseType => {
        // 删除与原有课程类型重复的课程类型
        data.splice(
          data.findIndex(item => item === courseType),
          1,
        );
      });
      this.courseTypeList = [...this.courseTypeList, ...data];
    });
  }

  /**
   * 弹射数据
   */
  ngModelChange($event: string) {
    this.courseTypeIdChange.emit($event);
  }

  ngOnInit() {
    this.getCourseTypeDropDownList();
  }
}
